package com.tanhua.server.controller;

import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.TodayBest;
import com.tanhua.server.service.MovementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private MovementsService movementsService;

    /**
     * 发布动态：
     *   POST
     *   请求参数：
     *     如果请求参数中包含图片，传递的数据不是json
     *     和Query参数类似
     *       imageContent : 图片文件
     *       textContent：文字内容
     *   不需要返回值
     */
    @PostMapping
    public ResponseEntity movements(MultipartFile[] imageContent,String textContent,String location,
                                    String longitude,String latitude) throws IOException {
        //1、获取请求参数
        Movement movement = new Movement();
        movement.setLongitude(longitude);
        movement.setLatitude(latitude);
        movement.setLocationName(location);
        movement.setTextContent(textContent);
        //2、调用service
        movementsService.movements(imageContent,movement);
        //3、返回
        return ResponseEntity.ok(null);
    }


    /**
     * 查询个人动态
     *  GET    /all
     *  Query：
     *      page，pagesize，userId
     *  返回值：
     *      PageResult
     *          List<MovementVo>
     */
    @GetMapping("/all")
    public ResponseEntity all(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pagesize,
                              Long userId) {
        PageResult vo = movementsService.findByUserId(userId,page,pagesize);
        return ResponseEntity.ok(vo);
    }

    /**
     * 查询好友动态：查询当前用户可见的其他人发布的动态列表
     * GET
     * Query：
     *   page，pagesize
     * 返回值：
     *     PageResult
     *         List<MovementVo>
     */
    @GetMapping
    public ResponseEntity movements(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult vo = movementsService.findByFriendId(page,pagesize);
        return ResponseEntity.ok(vo);
    }

    /**
     * 查询推荐动态
     *    GET  /recommend
     *  Query：
     *   page，pagesize
     *  返回值：
     *     PageResult
     *         List<MovementVo>
     */
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult vo = movementsService.findByRecommend(page,pagesize);
        return ResponseEntity.ok(vo);
    }

    /**
     * 根据id查询动态
     *      GET   /{id}
     *  返回值：
     *      MovementVo
     */
    @GetMapping("/{movementId}")
    public ResponseEntity findMovementById(@PathVariable("movementId") String movementId) {
        MovementsVo vo = movementsService.findMovementById(movementId);
        return ResponseEntity.ok(vo);
    }

    /**
     * 动态-点赞
     * 请求连接：
     *  GET /{movementId}/like
     *  响应数据
     *    点赞总数
     */
    @GetMapping("/{movementId}/like")
    public ResponseEntity like(@PathVariable("movementId") String movementId) {
        Integer count = movementsService.like(movementId);
        return ResponseEntity.ok(count);
    }

    /**
     * 取消点赞
     *   GET    /{movementId}/dislike
     *   响应数据
     *      点赞总数
     */
    @GetMapping("/{movementId}/dislike")
    public ResponseEntity dislike(@PathVariable("movementId") String movementId) {
        Integer count = movementsService.dislike(movementId);
        return ResponseEntity.ok(count);
    }


    //喜欢
    @GetMapping("/{movementId}/love")
    public ResponseEntity love(@PathVariable("movementId") String movementId) {
        Integer count = movementsService.love(movementId);
        return ResponseEntity.ok(count);
    }

    /**
     * 取消喜欢
     */
    @GetMapping("/{movementId}/unlove")
    public ResponseEntity unlove(@PathVariable("movementId") String movementId) {
        Integer count = movementsService.unlove(movementId);
        return ResponseEntity.ok(count);
    }

    /**
     * 查询访客（之后完成的功能）：配置一个空的方法。
     */
    @GetMapping("/visitors")
    public ResponseEntity visitors() {
        List<TodayBest> vos = movementsService.visitors();
        return ResponseEntity.ok(vos);
    }
}
